package cn.yeziji.forum.vo.user;

import cn.yeziji.forum.entity.ForumAnswerEntity;
import cn.yeziji.forum.entity.ForumDetailsEntity;
import cn.yeziji.forum.entity.ForumQuestionEntity;
import cn.yeziji.forum.vo.answer.AnswerStaticsVO;
import cn.yeziji.forum.vo.details.DetailStaticsVO;
import cn.yeziji.forum.vo.question.QuestionStaticsVO;
import lombok.Data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户统计文章视图对象
 *
 * @author gzkemays
 * @since 2022/11/26 5:53 PM
 */
@Data
public class UserStaticsDetailVO {
  Map<String, Integer> answerStatics = new HashMap<>();
  Map<String, Integer> questionStatics = new HashMap<>();
  Map<String, Integer> detailStatics = new HashMap<>();

  public UserStaticsDetailVO(
      List<ForumDetailsEntity> detailsEntities,
      List<ForumQuestionEntity> questionEntities,
      List<ForumAnswerEntity> answerEntities) {

    // 统计文章
    Map<String, List<DetailStaticsVO>> detailStatics =
        DetailStaticsVO.list(detailsEntities).stream()
            .collect(Collectors.groupingBy(DetailStaticsVO::getDate));
    for (Map.Entry<String, List<DetailStaticsVO>> entry : detailStatics.entrySet()) {
      this.detailStatics.put(entry.getKey(), entry.getValue().size());
    }
    // 统计问题
    Map<String, List<QuestionStaticsVO>> questionStatics =
        QuestionStaticsVO.list(questionEntities).stream()
            .collect(Collectors.groupingBy(QuestionStaticsVO::getDate));
    for (Map.Entry<String, List<QuestionStaticsVO>> entry : questionStatics.entrySet()) {
      this.questionStatics.put(entry.getKey(), entry.getValue().size());
    }
    // 统计答案
    Map<String, List<AnswerStaticsVO>> answerStatics =
        AnswerStaticsVO.list(answerEntities).stream()
            .collect(Collectors.groupingBy(AnswerStaticsVO::getDate));
    for (Map.Entry<String, List<AnswerStaticsVO>> entry : answerStatics.entrySet()) {
      this.answerStatics.put(entry.getKey(), entry.getValue().size());
    }
  }
}
